<template>
  <div class="app-container">
    <el-card>
      <el-form  ref="addForm" :model="addForm" label-width="120px" label-position="right" :rules="rules" size="small">
        <el-form-item label="文案" prop="content">
          <el-input style="width: 500px" type="textarea" maxlength="1000" show-word-limit :rows="4" placeholder="请输入文案"
            v-model="addForm.content"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <div style="display: flex">
            <single-upload uploadType="2" :maxCount="3" @uploadSucces="uploadSucces" v-model="addForm.imageUrl"
              :width="'100px'" :height="'100px'" :border="'1px solid #ccc'">
              <div class="uploadPic2" v-if="!addForm.imageUrl">
                <i style="font-size: 40px; color: #ccc; font-weight: bold" class="el-icon-plus"></i>
              </div>
            </single-upload>
            <div style="margin-top: 50px; color: #999">
              * 置顶文章需要上传封面图 (封面规格：350*120)
            </div>
          </div>
        </el-form-item>
        <el-form-item label="发布平台：" prop="platformType">
          <el-checkbox-group v-model="addForm.platformType">
            <el-checkbox v-for="city in platformTypeList" :label="city.dictValue" :key="city.dictValue">{{
              city.dictLabel }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button v-debounce :disabled="isDetail" type="primary" @click="onSubmit('addForm')">确定</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addToolLoans, getToolLoans, editToolLoans } from "@/api/tool";
export default {
  components: {
    SingleUpload: () => import("@/components/Upload/singleUpload"),
  },
  data() {
    return {
      platformTypeList: [],
      addForm: {
        content: "",
        imageUrl: [],
        platformType: ['2'],
        toolType: "material",
      },
      rules: {
        content: [{ required: true, message: "请输入文案", trigger: "blur" }],
        platformType: [
          { required: true, message: "请选择发布平台", trigger: "blur" },
        ],
      },
      isDetail: false
    };
  },
  async created() {
    this.platformTypeList = await this.$getDictData("platform_type");
    if (this.$route.query.id) {
      this.getInfo();
    }
    if (this.$route.query.id && this.$route.query.type == "detail") {
      this.$route.meta.title = "查看";
      this.isDetail = true
      this.getInfo();
    }
  },
  methods: {
    getInfo() {
      getToolLoans(this.$route.query.id).then((res) => {
        res.data.imageUrl = res.data.imageUrl.split(",");
        res.data.platformType = res.data.platformType.split(",");
        this.addForm = Object.assign({}, res.data);
      });
    },

    uploadSucces() { },
    goBack() {
      this.$router.go(-1);
    },
    onSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          let data = {
            content: this.addForm.content,
            imageUrl: this.addForm.imageUrl.join(","),
            platformType: this.addForm.platformType.join(","),
            toolType: "material",
          };

          if (this.addForm.id) {
            data.id = this.addForm.id;
            editToolLoans(data).then((res) => {
              this.$message.success("修改成功");
              this.$router.go(-1);
            });
          } else {
            addToolLoans(data).then((res) => {
              this.$message.success("添加成功");
              this.$router.go(-1);
            });
          }
        }
      });
    },
  },
};
</script>

<style></style>
